package groep3.ssc.coachViews;

import groep3.ssc.main.Main;
import groep3.ssc.main.R;
import groep3.ssc.skaterViews.SkaterViewTemplate;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.TextView;

public class SkateViewManagerAdapter extends ArrayAdapter<SkaterViewTemplate>
{
    private ArrayList<SkaterViewTemplate> items;
    private SkateViewManagerActivity skateViewManagerActivity;

    public SkateViewManagerAdapter(Context context, int textViewResourceId, ArrayList<SkaterViewTemplate> items,
            SkateViewManagerActivity onCheckedChangeListener)
    {
        super(context, textViewResourceId, items);
        this.items = items;
        this.skateViewManagerActivity = onCheckedChangeListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;

        if (view == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.skate_view_manager_list_item, null);
        }
        SkaterViewTemplate skaterView = items.get(position);
        if (skaterView != null)
        {
            TextView tt = (TextView) view.findViewById(R.id.magagerListItem);
            tt.setText(skaterView.toString());
            CheckBox cb1 = (CheckBox) view.findViewById(R.id.checkboxSkaterView1);
            cb1.setOnCheckedChangeListener(skateViewManagerActivity);
            cb1.setTag(skaterView);
            if (Main.skaterView1 == skaterView)
                cb1.setChecked(true);

            skateViewManagerActivity.addCheckBox(cb1);
            CheckBox cb2 = (CheckBox) view.findViewById(R.id.checkboxSkaterView2);
            cb2.setOnCheckedChangeListener(skateViewManagerActivity);
            cb2.setTag(skaterView);
            if (Main.skaterView2 == skaterView)
                cb2.setChecked(true);
            skateViewManagerActivity.addCheckBox(cb2);

            if (skaterView.hasSettings)
            {
                Spinner b = (Spinner) view.findViewById(R.id.spinnerSkaterViewSettings);
                b.setVisibility(View.VISIBLE);
                b.setOnItemSelectedListener(skateViewManagerActivity);
                ArrayAdapter<Object> adapter = skaterView.getSettingsAdapter(parent.getContext());
                b.setAdapter(adapter);
                b.setTag(skaterView);
                b.setSelection(adapter.getPosition(skaterView.getSettingsSelectedItem()), true);
            }
        }
        return view;
    }
}
